package com.bocai.eelly.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.NewsAdp;
import com.bocai.eelly.bean.NewsBean;
import com.bocai.eelly.common.BaseFragment;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.XListView;
import com.yyydjk.library.BannerLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 资讯列表
 */
public class InformationFragment extends BaseFragment implements XListView.IXListViewListener,AdapterView.OnItemClickListener{
    @Bind(R.id.lv_news)
    XListView lvNews;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    private View view;
    private int page=1;
    private NewsAdp newsAdp;
    private List<NewsBean.Data> news=new ArrayList<>();
    private BannerLayout bannerLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_information, null);
        initView();
        return view;

    }

    private void initView() {
        ButterKnife.bind(this, view);
        tvTitle.setText(R.string.str_title_news);
        getNewsDatas();
        newsAdp=new NewsAdp(getActivity(),news);
        lvNews.setAdapter(newsAdp);
        View banner=LayoutInflater.from(getActivity()).inflate(R.layout.news_banner,null);
        bannerLayout= (BannerLayout) banner.findViewById(R.id.banner);
//        bannerLayout.setViewUrls();
        lvNews.addHeaderView(banner);
        initEvent();
    }

    private void initEvent() {
        lvNews.setXListViewListener(this);
        lvNews.setPullLoadEnable(false);
        lvNews.setPullRefreshEnable(true);
        lvNews.setOnItemClickListener(this);
    }

    private void getNewsDatas() {
        Loading.show(getActivity(),getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().getNewsList(timeline,sign,Utils.getUserId(getActivity(),"id"),page+"",Utils.PAGESIZE+"")
                .compose(this.<NewsBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<NewsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        lvNews.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showShort(getActivity(),R.string.net_error);
                    }

                    @Override
                    public void onNext(NewsBean newsBean) {
                        lvNews.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(newsBean.getReturn_code()))
                        {
                            news.addAll(newsBean.getData());
                            newsAdp.notifyDataSetChanged();
                            if(newsBean.getData().size()<Utils.PAGESIZE){
                                lvNews.setPullLoadEnable(false);
                            }else{
                                lvNews.setPullLoadEnable(true);
                            }
                            if(page==1){
                                List<String> banners=new ArrayList<>();
                                for(int i=0;i<newsBean.getRec().size();i++)
                                {
                                    banners.add(newsBean.getRec().get(i).getPhoto());
                                }
                                bannerLayout.setViewUrls(banners);
                            }
                        }else
                        {
                            T.showShort(getActivity(),newsBean.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page=1;
        lvNews.setPullLoadEnable(false);
        news.clear();
        getNewsDatas();
    }

    @Override
    public void onLoadMore() {
        page++;
        getNewsDatas();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
